﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormMain
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim en As entity_cplh_ck
    Dim objUser As New clsUser
    Dim rb_checked_text As String = "2"
    Private objProdInfo As New clsProdInfo
    Private objProdXbInfo As New clsProdXbInfo
    Dim objCA As New clsCPLHCK
    Dim objPgdMes As New clsPgdMes
    Dim httpHelper As New HttpHelper
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        Dim reasons As String = GetIniFile("XB", "REASON", "", g_jsInitFileName)
        Dim reasonList As String() = reasons.Split("，")
        cmbReason.Items.Clear()
        cmbReason.Items.Add(String.Empty)
        For index = 0 To reasonList.Count() - 1
            cmbReason.Items.Add(reasonList(index))
        Next
        If cmbReason.Items.Count > 0 Then
            cmbReason.SelectedIndex = 0
        End If
        cmbResult.SelectedIndex = 0
        'Dim dtt As New DataTable
        'dtt.Columns.Add("name")
        'dtt.Columns.Add("val")
        'dtt.Rows.Add("是", "1")
        'dtt.Rows.Add("否", "0")
        'cmbFinal.DisplayMember = "name"
        'cmbFinal.ValueMember = "val"
        'cmbFinal.DataSource = dtt
        'cmbFinal.SelectedIndex = 0

        getUserListByCJ()
        txtBarCode.Select()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\xb.ini"
        g_jsInitFileName = My.Application.Info.DirectoryPath & "\js.ini"
        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(3, rb_checked_text)
        cmbOper.DisplayMember = "user_name"
        cmbOper.ValueMember = "user_id"
        cmbOper.DataSource = dtUser
    End Sub

    Private Sub getRbChechedText()
        If RadioButton4.Checked Then
            rb_checked_text = "2"
        End If
        If RadioButton5.Checked Then
            rb_checked_text = "3"
        End If
        If RadioButton6.Checked Then
            rb_checked_text = "4"
        End If
        If RadioButton1.Checked Then
            rb_checked_text = "1"
        End If
    End Sub

    Private Sub RadioButtonText_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton1.CheckedChanged
        getRbChechedText()
        getUserListByCJ()
    End Sub

    Private Sub cmbResult_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbResult.SelectedIndexChanged
        If cmbResult.SelectedIndex = 0 Then
            cmbReason.SelectedIndex = 0
            cmbReason.Enabled = False
            txtContent.Text = String.Empty
            txtContent.Enabled = False
        Else
            cmbReason.Enabled = True
            txtContent.Enabled = True
        End If
    End Sub

    Private Function search() As Integer
        Dim dt As DataTable = objProdInfo.getProdInfoByProdId(txtBarCode.Text.Trim())
        Dim dtdetails As DataTable = objProdXbInfo.getProdXbInfoByProdId(txtBarCode.Text.Trim())
        dtdetails.Columns.Add("xb_no_name")
        If dtdetails.Rows.Count > 0 Then
            For Each row As DataRow In dtdetails.Rows
                If row("xb_no") = "1" Then
                    row("xb_no_name") = "首次"
                Else
                    row("xb_no_name") = "检测后"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtdetails
        Return dt.Rows.Count
    End Function

    Private Sub txtBarCode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtBarCode.KeyPress
        If e.KeyChar = vbCr Then
            If String.IsNullOrEmpty(txtBarCode.Text.Trim()) = False Then
                Dim rtn_Count As Integer = search()
                If rtn_Count = 1 Then
                    SendKeys.Send("{Tab}")
                Else
                    If rtn_Count = 0 Then
                        MsgBox("此条码信息不存在")
                    Else
                        MsgBox("此条码信息存在多条记录")
                    End If
                End If
            Else
                MsgBox("请扫描或输入条码值")
            End If

        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If String.IsNullOrEmpty(cmbOper.Text.Trim()) Then
            MsgBox("请切换用户，以获取用户", vbYes, "提示")
            Exit Sub
        End If
        If String.IsNullOrEmpty(txtBarCode.Text.Trim()) Then
            MsgBox("请扫描或输入条码值", vbYes, "提示")
            Exit Sub
        End If
        If cmbResult.SelectedIndex > 1 Then
            If cmbReason.SelectedIndex = 0 Then
                MsgBox("请选择原因")
                Exit Sub
            Else
                If cmbReason.Text.Equals("其他") Then
                    If String.IsNullOrEmpty(txtContent.Text.Trim()) Then
                        MsgBox("请填写其他原因")
                        Exit Sub
                    End If
                End If
            End If
        End If
        Dim dtx As DataTable = objProdInfo.getProdInfoByProdIdAndStatus(txtBarCode.Text.Trim(), "7")
        If dtx.Rows.Count = 1 Then
            'Dim b_pgd_lh_details_sys_id As String = dtx.Rows(0)("b_pgd_lh_details_sys_id")
            saveData(1)
        Else
            Dim dtxb As DataTable = objProdInfo.getProdInfoByProdIdAndStatusBack(txtBarCode.Text.Trim(), "8")
            If dtxb.Rows.Count = 1 Then
                saveData(2)
            End If
            'If dgv.Rows.Count > 0 Then
            '    saveData()
            'End If
            'MsgBox("未查询到脱模完状态下的此生产编号信息", vbYes, "提示")
        End If
    End Sub

    Public Sub saveData(xb_no As Integer)
        If objProdXbInfo.setStatus(txtBarCode.Text.Trim(), cmbReason.Text.Trim(), txtContent.Text.Trim(), cmbResult.Text.Trim(), cmbDeviation.Text.Trim(), txtPYCC.Text.Trim(), cmbOper.Text.Trim(), 0, xb_no) <> -2 Then
            search()
            If cmbResult.Text.Contains("不") = False Then
                Dim dtt As DataTable = objCA.getListByProdId(txtBarCode.Text.Trim())
                If dtt.Rows.Count = 0 Then
                    Dim dtxt As DataTable = objProdInfo.getProdInfoByProdIdAndStatus(txtBarCode.Text.Trim(), "8")
                    'Dim dtpgdmeslh As DataTable = objPgdMes.getListBySysId(dtxt.Rows(0)("b_pgd_lh_details_sys_id").ToString())
                    'setEN(dtpgdmeslh.Rows(0)("b_pgd_mes_sys_id").ToString())
                    'Dim dtpgdmeslh As DataTable = objPgdMes.getListBySysId(dtxt.Rows(0)("pgd_id").ToString())
                    setEN(dtxt.Rows(0)("pgd_id").ToString())
                    If objCA.intoCK(en) = -2 Then
                        MsgBox("保存成功，入库失败", vbYes, "提示")
                    Else
                        MsgBox("保存成功，入库成功"， vbYes, "提示")
                    End If
                Else
                    MsgBox("保存成功！"， vbYes, "提示")
                End If
            Else
                MsgBox("保存成功", vbYes, "提示")
            End If
            txtBarCode.Text = String.Empty
            txtContent.Text = String.Empty
            cmbResult.SelectedIndex = 0
            cmbDeviation.SelectedIndex = 0
            'cmbFinal.SelectedIndex = 0
            txtBarCode.Focus()
            'If cmbFinal.SelectedValue.ToString().Equals("1") Then '最终结果

            'End If
        Else
            MsgBox("保存失败", vbYes, "提示")
        End If
    End Sub

    Public Sub setEN(b_pgd_mes_sys_id As String)
        en = New entity_cplh_ck
        With en
            .factory_id = "TS"
            .prod_id = txtBarCode.Text.Trim()
            .wh_no = String.Empty
            .b_pgd_mes_sys_id = b_pgd_mes_sys_id
            .into_ope = cmbOper.Text.Trim()
            .status = "0"
        End With
    End Sub

    Private Function mesbg(gtSysId As String, gtPgdMesSysId As String, oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As Integer
        Dim rtn_int As Integer
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "喷涂" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("dispatchingOrderBG", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            'rtn_int = objGtPgdMesWorkReport.insert("ok", gtSysId, gtPgdMesSysId, oper_num)
        End If
        Return rtn_int
    End Function

    Private Sub txt_MouseDown(sender As Object, e As MouseEventArgs) Handles txtPYCC.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If
        UcDigitalKeyboard1.B_02 = True
        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub ucdigitalkeyboard1_leave(sender As Object, e As EventArgs) Handles UcDigitalKeyboard1.Leave
        UcDigitalKeyboard1.Dispose()
    End Sub


    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

    Private Sub txtContent_Click(sender As Object, e As EventArgs) Handles txtContent.Click
        Process.Start("C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe")
        'Process.Start("C:\windows\system32\osk.exe")
    End Sub

End Class

Public Class entity_cplh_ck
    Public sys_id As String
    Public factory_id As String
    Public wh_no As String
    Public prod_id As String
    Public b_pgd_mes_sys_id As String
    Public into_ope As String
    Public status As String
End Class

